package entity

import (
	"fmt"
	"go-yuque/transform"
	"sync"
	"time"
)

// type DocTreeNodeStruct struct {
// 	CurrentToc  *TocStruct
// 	Children    []*TocStruct
// 	PreNode     *TocStruct
// 	SiblingNode *TocStruct
// }

type TocStruct struct {
	Type         string                   `json:"type"`
	Title        string                   `json:"title"`
	UUID         string                   `json:"uuid"`
	URL          string                   `json:"url"`
	PrevUUID     string                   `json:"prev_uuid"`
	SiblingUUID  string                   `json:"sibling_uuid"`
	ChildUUID    string                   `json:"child_uuid"`
	ParentUUID   string                   `json:"parent_uuid"`
	DocID        transform.StringToUint64 `json:"doc_id"`
	Level        int                      `json:"level"`
	ID           transform.StringToUint64 `json:"id"`
	OpenWindow   int                      `json:"open_window"`
	Visible      int                      `json:"visible"`
	Children     []*TocStruct             `json:"children"`
	ChildToc     *TocStruct               `json:"-"` // json时忽略该字段
	PreToc       *TocStruct               `json:"-"` // json时忽略该字段
	SiblingToc   *TocStruct               `json:"-"` // json时忽略该字段
	SequenceNo   string                   `json:"sequenceNo" name:"序号"`
	Path         string                   `json:"path"`
	handleResult string                   `name:"处理结果"`
	mx           sync.Mutex
}

func (t *TocStruct) Init() {
	t.mx = sync.Mutex{}
}

func (t *TocStruct) AddHandleResult(res string, isError bool) {
	if len(res) <= 0 {
		return
	}

	t.mx.Lock()
	defer func() {
		t.mx.Unlock()
	}()

	if isError {
		res = fmt.Sprintf("<span style=\"color: orangered; font-weight: bolder\">%s</span>", res)
	}

	if len(t.handleResult) > 0 {
		t.handleResult += ";" + res
	} else {
		t.handleResult = res
	}
}

func (t *TocStruct) GetHandleResult() string {
	t.mx.Lock()
	defer func() {
		t.mx.Unlock()
	}()

	return t.handleResult
}

// YuqueBookStruct 单个知识库结构体
type YuqueBookStruct struct {
	Book struct {
		ID           int         `json:"id"`
		Type         string      `json:"type"`
		Slug         string      `json:"slug"`
		Name         string      `json:"name"`
		Toc          []TocStruct `json:"toc"`
		TocUpdatedAt time.Time   `json:"toc_updated_at"`
		Description  string      `json:"description"`
		CreatorID    int         `json:"creator_id"`
		MenuType     int         `json:"menu_type"`
		ItemsCount   int         `json:"items_count"`
		LikesCount   int         `json:"likes_count"`
		WatchesCount int         `json:"watches_count"`
		UserID       int         `json:"user_id"`
		Abilities    struct {
			CreateDoc          bool `json:"create_doc"`
			Destroy            bool `json:"destroy"`
			Export             bool `json:"export"`
			ExportDoc          bool `json:"export_doc"`
			Read               bool `json:"read"`
			ReadPrivate        bool `json:"read_private"`
			Update             bool `json:"update"`
			CreateCollaborator bool `json:"create_collaborator"`
			Manage             bool `json:"manage"`
			Share              bool `json:"share"`
			ModifySetting      bool `json:"modify_setting"`
		} `json:"abilities"`
		Public                  int         `json:"public"`
		ExtendPrivate           int         `json:"extend_private"`
		Scene                   interface{} `json:"scene"`
		Source                  interface{} `json:"source"`
		CreatedAt               time.Time   `json:"created_at"`
		UpdatedAt               time.Time   `json:"updated_at"`
		PinnedAt                interface{} `json:"pinned_at"`
		ArchivedAt              interface{} `json:"archived_at"`
		Layout                  string      `json:"layout"`
		DocTypography           string      `json:"doc_typography"`
		DocViewport             string      `json:"doc_viewport"`
		Announcement            interface{} `json:"announcement"`
		Namespace               string      `json:"namespace"`
		ShouldManuallyCreateUID bool        `json:"should_manually_create_uid"`
		CatalogTailType         string      `json:"catalog_tail_type"`
		CatalogDisplayLevel     int         `json:"catalog_display_level"`
		Cover                   string      `json:"cover"`
		CoverColor              string      `json:"cover_color"`
		CommentCount            interface{} `json:"comment_count"`
		OrganizationID          int         `json:"organization_id"`
		Status                  int         `json:"status"`
		IndexedLevel            int         `json:"indexed_level"`
		PrivacyMigrated         bool        `json:"privacy_migrated"`
		CollaborationCount      int         `json:"collaboration_count"`
		ContentUpdatedAt        time.Time   `json:"content_updated_at"`
		ContentUpdatedAtMs      int64       `json:"content_updated_at_ms"`
		CopyrightWatermark      string      `json:"copyright_watermark"`
		EnableAnnouncement      bool        `json:"enable_announcement"`
		EnableAutoPublish       bool        `json:"enable_auto_publish"`
		EnableAutomation        bool        `json:"enable_automation"`
		DocCreateLocation       string      `json:"doc_create_location"`
		EnableComment           bool        `json:"enable_comment"`
		EnableDocumentCopy      bool        `json:"enable_document_copy"`
		EnableExport            bool        `json:"enable_export"`
		EnableSearchEngine      bool        `json:"enable_search_engine"`
		EnableToc               bool        `json:"enable_toc"`
		EnableTrash             bool        `json:"enable_trash"`
		EnableVisitorWatermark  bool        `json:"enable_visitor_watermark"`
		EnableWebhook           bool        `json:"enable_webhook"`
		EnableLike              bool        `json:"enable_like"`
		ImageCopyrightWatermark string      `json:"image_copyright_watermark"`
		Original                int         `json:"original"`
		ResourceSize            int         `json:"resource_size"`
		Password                interface{} `json:"password"`
		User                    interface{} `json:"user"`
		Contributors            interface{} `json:"contributors"`
		Serializer              string      `json:"_serializer"`
	} `json:"book"`
}

// YuqueCategoryBooksStruct 所有类别与知识库结构体
type YuqueCategoryBooksStruct struct {
	Status int `json:"status"`
	Data   []struct {
		ID               int         `json:"id"`
		CreatedAt        time.Time   `json:"created_at"`
		UpdatedAt        time.Time   `json:"updated_at"`
		UserID           int         `json:"user_id"`
		DisplayType      int         `json:"display_type"`
		Name             string      `json:"name"`
		Rank             int         `json:"rank"`
		IsDefault        interface{} `json:"is_default"`
		OrganizationID   int         `json:"organization_id"`
		ShowBookIcon     int         `json:"show_book_icon"`
		ShowBookDesc     int         `json:"show_book_desc"`
		ShowDocUpdatedAt int         `json:"show_doc_updated_at"`
		DocOrderType     int         `json:"doc_order_type"`
		Books            []struct {
			ID           int    `json:"id"`
			Type         string `json:"type"`
			Slug         string `json:"slug"`
			Name         string `json:"name"`
			UserID       int    `json:"user_id"`
			Description  string `json:"description"`
			ItemsCount   int    `json:"items_count"`
			LikesCount   int    `json:"likes_count"`
			WatchesCount int    `json:"watches_count"`
			CreatorID    int    `json:"creator_id"`
			Abilities    struct {
				Read          bool `json:"read"`
				Update        bool `json:"update"`
				ModifySetting bool `json:"modify_setting"`
				Destroy       bool `json:"destroy"`
				Share         bool `json:"share"`
				ReadPrivate   bool `json:"read_private"`
				CreateDoc     bool `json:"create_doc"`
			} `json:"abilities"`
			Public             int         `json:"public"`
			ExtendPrivate      int         `json:"extend_private"`
			Scene              interface{} `json:"scene"`
			Source             interface{} `json:"source"`
			CreatedAt          time.Time   `json:"created_at"`
			UpdatedAt          time.Time   `json:"updated_at"`
			ContentUpdatedAt   time.Time   `json:"content_updated_at"`
			PinnedAt           interface{} `json:"pinned_at"`
			ArchivedAt         interface{} `json:"archived_at"`
			Status             int         `json:"status"`
			StackID            int         `json:"stack_id"`
			Rank               int         `json:"rank"`
			Layout             string      `json:"layout"`
			DocViewport        string      `json:"doc_viewport"`
			DocTypography      string      `json:"doc_typography"`
			Cover              string      `json:"cover"`
			CoverColor         string      `json:"cover_color"`
			CommentCount       interface{} `json:"comment_count"`
			ReadCount          interface{} `json:"read_count"`
			Original           int         `json:"original"`
			OrganizationID     int         `json:"organization_id"`
			EnableAutoPublish  bool        `json:"enable_auto_publish"`
			EnableAutomation   bool        `json:"enable_automation"`
			DocCreateLocation  string      `json:"doc_create_location"`
			PrivacyMigrated    bool        `json:"privacy_migrated"`
			EnableAnnouncement bool        `json:"enable_announcement"`
			Announcement       interface{} `json:"announcement"`
			User               struct {
				ID               int         `json:"id"`
				Type             string      `json:"type"`
				Login            string      `json:"login"`
				Name             string      `json:"name"`
				Description      string      `json:"description"`
				Avatar           string      `json:"avatar"`
				AvatarURL        string      `json:"avatar_url"`
				FollowersCount   int         `json:"followers_count"`
				FollowingCount   int         `json:"following_count"`
				Status           int         `json:"status"`
				Public           int         `json:"public"`
				Scene            interface{} `json:"scene"`
				CreatedAt        time.Time   `json:"created_at"`
				UpdatedAt        time.Time   `json:"updated_at"`
				ExpiredAt        time.Time   `json:"expired_at"`
				IsPaid           bool        `json:"isPaid"`
				MemberLevel      int         `json:"member_level"`
				MemberLevelName  string      `json:"memberLevelName"`
				HasMemberLevel   bool        `json:"hasMemberLevel"`
				IsTopLevel       bool        `json:"isTopLevel"`
				IsNewbie         bool        `json:"isNewbie"`
				Profile          interface{} `json:"profile"`
				OrganizationUser interface{} `json:"organizationUser"`
				Serializer       string      `json:"_serializer"`
			} `json:"user"`
			Creator    interface{} `json:"creator"`
			Serializer string      `json:"_serializer"`
		} `json:"books"`
		Serializer string `json:"_serializer"`
	} `json:"data"`
}
